#include "llbjm.h"
#include "ui_llbjm.h"
#include <chzhi.h>

llbjm::llbjm(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::llbjm)
{
    ui->setupUi(this);

    connect(ui->pushButton, &QPushButton::clicked, [=]() {
        ui->stackedWidget->setCurrentIndex(1);
    });
    connect(ui->pushButton_2, &QPushButton::clicked, [=]() {
        ui->stackedWidget->setCurrentIndex(2);
    });
    connect(ui->pushButton_4, &QPushButton::clicked, [=]() {
        ui->stackedWidget->setCurrentIndex(3);
    });
    connect(ui->pushButton_3, &QPushButton::clicked, [=]() {
        ui->stackedWidget->setCurrentIndex(0);
    });

    //设置表格的列数
    ui->tableWidget->setColumnCount(3);
    //设置列宽
    ui->tableWidget->setColumnWidth(0,168);
    ui->tableWidget->setColumnWidth(1,168);
    ui->tableWidget->setColumnWidth(1,168);
    //设置表格字段名
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"订单："<<"商品："<<"商品数量");
    //设置表格的行数
    ui->tableWidget->setRowCount(20);


}

llbjm::~llbjm()
{
    delete ui;
}

void llbjm::on_pushButton_7_clicked()
{
    chzhi *ch = new chzhi;
    ch->username = string;

    ch->show();


    //this->close();

}
extern QSqlDatabase db;
void llbjm::on_pushButton_9_clicked()
{

    QString username = string;
    QString name = ui->pushButton_23->text();
    QString name1 = ui->pushButton_22->text();
    QString name2 = ui->pushButton_24->text();
    QString name3 = ui->pushButton_20->text();
    QString name4 = ui->pushButton_13->text();
    QString name5 = ui->pushButton_19->text();

    int jiage = ui->pushButton_12->text().toInt(); // 将按钮文本转换为 int
    int jiagee = ui->spinBox_13->value();
    int jiage11 = ui->spinBox_12->value();
    int jiage22 = ui->spinBox_14->value();
    int jiage33 = ui->spinBox_10->value();
    int jiage4 = ui->spinBox_4->value();
    int jiage5 = ui->spinBox_9->value();
    QString str;
    QString sql = QString("SELECT * FROM user WHERE username = '%1'").arg(username);

    QSqlQuery query(db);
    bool ret = query.exec(sql);
    if (!ret) {
        qDebug() << "查询失败:" ;
    } else {
        while (query.next()) {
            str = query.value("balance").toString();
            qDebug()<<str;
        }
    }
    bool ok;
    int jiage1 = str.toInt(&ok);
    int jiage2 = jiage1 - jiage;

    qDebug()<<jiage2;
    if(jiage2 < 0){
        QMessageBox::information(this,"余额","余额不足");
        return;
    }
    QString cmd = QString("update user set balance=%1 where username='%2';")
                      .arg(jiage2).arg(username);
    qDebug()<<cmd;
    QSqlQuery query1(db);
    bool ret1 = query1.exec(cmd);
    if(!ret1){
        qDebug()<<"修改失败";
        return;
    }
    qDebug()<<"nij"<<jiagee;
    if(jiagee > 0){
        QString cha = QString("INSERT INTO llbcai (username, name, quantity,price) VALUES ('%1','%2',%3,%4);")
        .arg(username).arg(name).arg(jiagee).arg(1.1);
        qDebug()<<cha;
        QSqlQuery query2(db);
        bool ret2 = query2.exec(cha);
        if(!ret2){
            qDebug()<<"插入1失败";
            return;
        }
    }
    if(jiage11 > 0){
        QString cha = QString("INSERT INTO llbcai (username, name, quantity,price) VALUES ('%1','%2',%3,%4);")
        .arg(username).arg(name1).arg(jiage11).arg(1.1);
        qDebug()<<cha;
        QSqlQuery query2(db);
        bool ret2 = query2.exec(cha);
        if(!ret2){
            qDebug()<<"插入2失败";
            return;
        }
    }
    if(jiage22 > 0){
        QString cha = QString("INSERT INTO llbcai (username, name, quantity,price) VALUES ('%1','%2',%3,%4);")
        .arg(username).arg(name2).arg(jiage22).arg(1.1);
        qDebug()<<cha;
        QSqlQuery query2(db);
        bool ret2 = query2.exec(cha);
        if(!ret2){
            qDebug()<<"插入3失败";
            return;
        }
    }
    if(jiage33 > 0){
        QString cha = QString("INSERT INTO llbcai (username, name, quantity,price) VALUES ('%1','%2',%3,%4);")
        .arg(username).arg(name3).arg(jiage33).arg(1.1);
        qDebug()<<cha;
        QSqlQuery query2(db);
        bool ret2 = query2.exec(cha);
        if(!ret2){
            qDebug()<<"插入4失败";
            return;
        }
    }
    if(jiage4 > 0){
        QString cha = QString("INSERT INTO llbcai (username, name, quantity,price) VALUES ('%1','%2',%3,%4);")
        .arg(username).arg(name4).arg(jiage4).arg(1.1);
        qDebug()<<cha;
        QSqlQuery query2(db);
        bool ret2 = query2.exec(cha);
        if(!ret2){
            qDebug()<<"插入5失败";
            return;
        }
    }
    if(jiage5 > 0){
        QString cha = QString("INSERT INTO llbcai (username, name, quantity,price) VALUES ('%1','%2',%3,%4);")
        .arg(username).arg(name5).arg(jiage5).arg(1.1);
        qDebug()<<cha;
        QSqlQuery query2(db);
        bool ret2 = query2.exec(cha);
        if(!ret2){
            qDebug()<<"插入6失败";
            return;
        }
    }


    ui->pushButton_12->setText("");
    ui->spinBox_9->setValue(0);
    ui->spinBox_4->setValue(0);
    ui->spinBox_10->setValue(0);
    ui->spinBox_14->setValue(0);
    ui->spinBox_13->setValue(0);
    ui->spinBox_12->setValue(0);
}

void llbjm::on_spinBox_13_textChanged(const QString &arg1)
{
    // sqlite_Init();
    QString username = string;
    QString name = ui->pushButton_23->text();
    int quantity1 = ui->spinBox_12->value();
    int quantity = ui->spinBox_13->value();
    int quantity2 = ui->spinBox_14->value();
    int quantity3 = ui->spinBox_10->value();
    int quantity4 = ui->spinBox_4->value();
    int quantity5 = ui->spinBox_9->value();
    int price = quantity1 * 18 + quantity * 10 + quantity2 * 22 + quantity3 * 10 + quantity4 * 18 + quantity5 * 22;

    //ui->pushButton->text() = quantity;
    ui->pushButton_12->setText(QString::number(price));
}

void llbjm::on_spinBox_12_textChanged(const QString &arg1)
{
    // sqlite_Init();
    QString username = string;
    QString name = ui->pushButton_22->text();
    int quantity1 = ui->spinBox_12->value();
    int quantity = ui->spinBox_13->value();
    int quantity2 = ui->spinBox_14->value();
    int quantity3 = ui->spinBox_10->value();
    int quantity4 = ui->spinBox_4->value();
    int quantity5 = ui->spinBox_9->value();
    int price = quantity1 * 18 + quantity * 10 + quantity2 * 22 + quantity3 * 10 + quantity4 * 18 + quantity5 * 22;

    //ui->pushButton->text() = quantity;
    ui->pushButton_12->setText(QString::number(price));
}



void llbjm::on_spinBox_14_textChanged(const QString &arg1)
{
    // sqlite_Init();
    QString username = string;
    QString name = ui->pushButton_24->text();
    int quantity1 = ui->spinBox_12->value();
    int quantity = ui->spinBox_13->value();
    int quantity2 = ui->spinBox_14->value();
    int quantity3 = ui->spinBox_10->value();
    int quantity4 = ui->spinBox_4->value();
    int quantity5 = ui->spinBox_9->value();
    int price = quantity1 * 18 + quantity * 10 + quantity2 * 22 + quantity3 * 10 + quantity4 * 18 + quantity5 * 22;

    //ui->pushButton->text() = quantity;
    ui->pushButton_12->setText(QString::number(price));
}


void llbjm::on_spinBox_10_textChanged(const QString &arg1)
{
    QString username = string;
    QString name = ui->pushButton_20->text();
    int quantity1 = ui->spinBox_12->value();
    int quantity = ui->spinBox_13->value();
    int quantity2 = ui->spinBox_14->value();
    int quantity3 = ui->spinBox_10->value();
    int quantity4 = ui->spinBox_4->value();
    int quantity5 = ui->spinBox_9->value();
    int price = quantity1 * 18 + quantity * 10 + quantity2 * 22 + quantity3 * 10 + quantity4 * 18 + quantity5 * 22;

    //ui->pushButton->text() = quantity;
    ui->pushButton_12->setText(QString::number(price));
}


void llbjm::on_spinBox_4_textChanged(const QString &arg1)
{
    QString username = string;
    QString name = ui->pushButton_13->text();
    int quantity1 = ui->spinBox_12->value();
    int quantity = ui->spinBox_13->value();
    int quantity2 = ui->spinBox_14->value();
    int quantity3 = ui->spinBox_10->value();
    int quantity4 = ui->spinBox_4->value();
    int quantity5 = ui->spinBox_9->value();
    int price = quantity1 * 18 + quantity * 10 + quantity2 * 22 + quantity3 * 10 + quantity4 * 18 + quantity5 * 22;

    //ui->pushButton->text() = quantity;
    ui->pushButton_12->setText(QString::number(price));
}


void llbjm::on_spinBox_9_textChanged(const QString &arg1)
{
    QString username = string;
    QString name = ui->pushButton_19->text();
    int quantity1 = ui->spinBox_12->value();
    int quantity = ui->spinBox_13->value();
    int quantity2 = ui->spinBox_14->value();
    int quantity3 = ui->spinBox_10->value();
    int quantity4 = ui->spinBox_4->value();
    int quantity5 = ui->spinBox_9->value();
    int price = quantity1 * 18 + quantity * 10 + quantity2 * 22 + quantity3 * 10 + quantity4 * 18 + quantity5 * 22;

    //ui->pushButton->text() = quantity;
    ui->pushButton_12->setText(QString::number(price));
}


void llbjm::on_pushButton_4_clicked()
{
    int j = 0;
    QString sql = QString("SELECT * FROM llbcai WHERE username = '%1'").arg(string);

    QSqlQuery query(db);
    bool ret = query.exec(sql);

    ui->tableWidget->clear();//把表格列表框清空
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"订单号："<<"商品："<<"商品数量：");

    if (!ret) {
        qDebug() << "查询失败:" ;
    } else {
        while (query.next()) {
            for(int i = 0;i < 3;i++){


            QString str = query.value(i).toString();
            qDebug()<<str;
            QTableWidgetItem * item = new QTableWidgetItem(str);
            ui->tableWidget->setItem(j,i,item);


        }
            j ++;
        }
    }
}


void llbjm::on_pushButton_clicked()
{

}


void llbjm::on_pushButton_5_clicked()
{

    this->close();

}

